home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 3 / CD ACTUAL 3.iso / linux / incoming / jstools-.6v3 / jstools- / jstools-tk3.6v3.0 / lib / jfs.tcl < prev    next >
Encoding:
Text File  |  1995-02-09  |  8.7 KB  |  326 lines

  1. # jfs.tcl - file-selection panel
  2. # Copyright 1992-1994 by Jay Sekora.  All rights reserved, except 
  3. # that this file may be freely redistributed in whole or in part 
  4. # for non-profit, noncommercial use.
  5. # these procedures are required by (at least)
  6. #     browser.tk
  7. #     edit.tk
  8. #     more.tk
  9. #     people.tk
  10. ######################################################################
  11.  
  12. ### TO DO
  13. ###   more error-checking in j:fs
  14. ###   fix focus on j:fs
  15. ###   option for load/save?
  16. ###   mkdir when saving?
  17. ###   `default' behaviour needs fixed (do we even need a default now?)
  18.  
  19. ### CHANGES
  20. ###   j:fs now no longer does a cd (well, it does, but it un-does it)
  21.  
  22. ######################################################################
  23. # global variables:
  24. #
  25. global J_PREFS env
  26. if {! [info exists J_PREFS(autoposition)]} {set J_PREFS(autoposition) 0}
  27. if {! [info exists J_PREFS(confirm)]} {set J_PREFS(confirm) 1}
  28. #
  29. ######################################################################
  30.  
  31. ######################################################################
  32. # j:fs ?options? - file selector box
  33. # options are:
  34. #   -buttons (default {ok cancel home})
  35. #   -prompt (default "Choose a file")
  36. #   -directory (default ".")
  37. #   -cancelvalue (default "")
  38. #   -fileprompt (default "File:")
  39. #   -title (default "File Selector")
  40. #   -types (default "")
  41. #   -typevariable (default "")
  42. # NOTE: this may do a cd---affects entire app!
  43. ######################################################################
  44. ### this proc is too monolithic; it should be broken up.
  45.  
  46. proc j:fs { args } {
  47.   j:parse_args {
  48.     {buttons {ok cancel home} }
  49.     {prompt "Choose a file"}
  50.     {directory "."}
  51.     {cancelvalue ""}
  52.     {fileprompt "File:"}
  53.     {title "File Selector"}
  54.     {types ""}
  55.     {typevariable ""}
  56.     {typeprompt "File type:"}
  57.   }
  58.     
  59.   global j_fs env J_PREFS
  60.   global fs_defaultbutton
  61.   set J_PREFS(0) 1        ;# make sure it's intepreted as array
  62.   
  63.   if {[lsearch [array names J_PREFS] {j_fs_fast}] == -1} {
  64.     set J_PREFS(j_fs_fast) 0    ;# make sure it's defined
  65.   }
  66.   if {[lsearch [array names J_PREFS] {scrollbarside}] == -1} {
  67.     set J_PREFS(scrollbarside) right ;# make sure it's defined
  68.   }
  69.   
  70.   set old_cwd [pwd]        ;# save current directory to un-do cd's
  71.   
  72.   set dir $directory
  73.   set file ""
  74.  
  75.   if {![file isdirectory $dir]} {
  76.     set dir .
  77.   }
  78.  
  79.   set fs_defaultbutton [lindex $buttons 0]
  80.  
  81.   set j_fs(result) $file
  82.   set j_fs(type) {}
  83.   
  84.   j:tk3 {
  85.     set old_focus [focus]        ;# so we can restore original focus
  86.   }
  87.   j:tk4 {
  88.     set old_focus [focus -lastfor .]    ;# so we can restore original focus
  89.   }
  90.   
  91.   if [winfo exists .fs] {
  92.     destroy .fs
  93.   }
  94.  
  95.   cd $dir
  96.  
  97.   toplevel .fs
  98.   wm title .fs $title
  99.   wm minsize .fs 10 10
  100.  
  101.   label .fs.prompt -anchor w -text $prompt
  102.   label .fs.cwd -text [pwd]
  103.   frame .fs.list
  104.   listbox .fs.list.lb -yscroll ".fs.list.sb set"
  105.   j:tk3 {.fs.list.lb configure -geometry 30x20}
  106.   j:tk4 {.fs.list.lb configure -width 30 -height 20}
  107.   scrollbar .fs.list.sb -relief flat -command ".fs.list.lb yview"
  108.   frame .fs.file
  109.   label .fs.file.l -text $fileprompt -anchor e
  110.   entry .fs.file.e -relief sunken -text $file
  111.   
  112.   if {"x$types" != "x" && "x$typevariable" != "x"} {
  113.     frame .fs.type
  114.     label .fs.type.l -text $typeprompt -anchor e
  115.     j:option .fs.type.o -list $types
  116.     pack .fs.type.l -side left -pady 10 -padx 10
  117.     pack .fs.type.o -side left -expand yes -pady 10 -padx 10 -fill x
  118.   }
  119.  
  120.   frame .fs.b -width 200
  121.   button .fs.b.ok -width 8 -text {OK} -command {
  122.     set file [.fs.file.e get]
  123.     if {[file isdirectory ./$file]} {
  124.       cd $file            ;# cd into directory, refresh list
  125.       .fs.cwd configure -text [pwd]
  126.       j:fs:fill_list .fs.list.lb
  127.       .fs.file.e delete 0 end    ;# clear filename space
  128.     } else {
  129.       set cwd [pwd]
  130.       if {$cwd == "/"} {set cwd ""}
  131.       set file [.fs.file.e get]
  132.       case $file in {
  133.         /*    {set j_fs(result) $file}
  134.         default {set j_fs(result) $cwd/$file}
  135.       }
  136.       if [winfo exists .fs.type.o] {
  137.         set j_fs(type) [.fs.type.o get]
  138.       }
  139.       
  140.       destroy .fs
  141.       update
  142.     }
  143.   }
  144.   button .fs.b.gointo -width 8 -text "Go Into" -command {
  145.     set file [.fs.file.e get]
  146.     if {[file isdirectory ./$file]} {
  147.       cd $file            ;# cd into directory, refresh list
  148.       .fs.cwd configure -text [pwd]
  149.       j:fs:fill_list .fs.list.lb
  150.       .fs.file.e delete 0 end    ;# clear filename space
  151.     } else {
  152.       j:alert -text "\"$file\" is not a directory."
  153.     }
  154.   }
  155.   button .fs.b.home -width 8 -text {Home} -command {
  156.     cd $env(HOME)
  157.     .fs.cwd configure -text [pwd]
  158.     j:fs:fill_list .fs.list.lb
  159.   }
  160.   button .fs.b.root -width 8 -text {Root} -command {
  161.     cd /
  162.     .fs.cwd configure -text [pwd]
  163.     j:fs:fill_list .fs.list.lb
  164.   }
  165.   button .fs.b.here -width 8 -text {Here} -command {
  166.     set j_fs(result) [pwd]
  167.     
  168.     # need for following is probably pretty rare:
  169.     if [winfo exists .fs.type.o] {
  170.       set j_fs(type) [.fs.type.o get]
  171.     }
  172.       
  173.  
  174.     destroy .fs
  175.     update
  176.   }
  177.   button .fs.b.cancel -width 8 -text {Cancel} -command "
  178.     set j_fs(result) $cancelvalue
  179.     destroy .fs
  180.     update
  181.   "
  182.   checkbutton .fs.b.fast -text {Fast} -relief flat \
  183.     -variable J_PREFS(j_fs_fast)
  184.  
  185.   pack .fs.list.sb -side $J_PREFS(scrollbarside) -fill y
  186.   pack [j:rule .fs.list] -side $J_PREFS(scrollbarside) -fill y
  187.   pack .fs.list.lb -side left -expand yes -fill both
  188.   
  189.   pack .fs.file.l -side left -pady 10 -padx 10
  190.   pack .fs.file.e -side left -expand yes -pady 10 -padx 10 -fill x
  191.   pack [j:filler .fs.file] -side left
  192.   
  193.   # now create the buttons the caller requested:
  194.   #    (NEEDS ERROR CHECKING!)
  195.   pack [j:filler .fs.b] -side bottom
  196.   pack .fs.b.fast -side top
  197.   foreach b $buttons {
  198.     set button .fs.b.$b
  199.     set border .fs.b.border_$b
  200.     frame $border -borderwidth 1 -relief flat
  201.     raise $button
  202.     pack $button -in $border -padx 2 -pady 2
  203.     pack $border -in .fs.b -side bottom -padx 10 -pady 4
  204.   }
  205.   # wider border on default button:
  206.   .fs.b.border_$fs_defaultbutton configure -relief sunken
  207.  
  208.   pack .fs.prompt -side top -fill both
  209.   pack [j:rule .fs] -side top -fill x
  210.   pack .fs.cwd -side top -fill both
  211.   pack [j:rule .fs] -side top -fill x
  212.   pack .fs.file -side bottom -expand yes -fill x
  213.   pack [j:rule .fs] -side bottom -fill x
  214.   if [winfo exists .fs.type] {
  215.     pack .fs.type -side bottom -expand yes -fill x
  216.     pack [j:rule .fs] -side bottom -fill x
  217.   }
  218.   pack \
  219.     .fs.b \
  220.     [j:rule .fs] \
  221.     -side right -fill y
  222.   pack .fs.list -side top -expand yes -fill both
  223.  
  224.   j:dialogue .fs        ;# position in centre of screen
  225.  
  226.   .fs.file.e insert end $j_fs(result)
  227.  
  228.   focus .fs.file.e
  229.   bind .fs.file.e <Key-Return> {
  230.     set file [.fs.file.e get]
  231.     if {$file != {} && [file isdirectory ./$file]} {
  232.       .fs.b.gointo invoke
  233.     } else {
  234.       .fs.b.$fs_defaultbutton invoke
  235.     }
  236.   }
  237.   bind .fs.file.e <Key-Tab> {    ;# expand filename on <Tab>
  238.     set f [%W get]
  239.     %W delete 0 end
  240.     %W insert end [j:expand_filename $f]
  241.   }
  242.   bind .fs.list.lb <Button-1> {    ;# select, and insert filename into entry
  243.     j:tk3 {
  244.       %W select from [%W nearest %y]
  245.     }
  246.     j:tk4 {
  247.       %W selection clear 0 end; %W selection set [%W nearest %y]
  248.     }
  249.     set file [lindex [selection get] 0]
  250.     .fs.file.e delete 0 end
  251.     .fs.file.e insert end $file
  252.   }
  253.  
  254.   bind .fs.list.lb <Double-Button-1> {    ;# cd to dir or do default thing
  255.     set file [lindex [j:selection_if_any] 0]
  256.     if [file isdirectory ./$file] {
  257.       .fs.b.gointo invoke
  258.     } else {
  259.       .fs.b.$fs_defaultbutton invoke
  260.     }
  261.   }
  262.   
  263.   j:cancel_button .fs.b.cancel .fs.file.e
  264.  
  265. #  grab .fs            ;# for some reason this screws up 
  266.                 ;#   "bind .fs.list.lb <Double-Button-1> ..."
  267.  
  268.   j:fs:fill_list .fs.list.lb    ;# fill the listbox for the first time
  269.   tkwait window .fs
  270.   cd $old_cwd            ;# leave application in original dir.
  271.   focus $old_focus
  272.   
  273.   if {"x$types" != "x" && "x$typevariable" != "x"} {
  274.     global OPTION_FOR_.fs.option.o
  275.     uplevel 1 [list set $typevariable $j_fs(type)]
  276.   }
  277.   
  278.   return $j_fs(result)
  279. }
  280.  
  281. ######################################################################
  282. # j:fs:fill_list lb - fill the listbox with files from CWD
  283. ######################################################################
  284.  
  285. proc j:fs:fill_list {lb} {
  286.   global J_PREFS
  287.   set J_PREFS(0) 1
  288.   $lb delete 0 end
  289.  
  290.   # add ".." to go up a level:
  291.   $lb insert end ".."
  292.  
  293.   update
  294.  
  295.   # add all normal (non-dot) files:
  296.   foreach i [lsort [glob -nocomplain *]] {
  297.     if { ! $J_PREFS(j_fs_fast) } {
  298.       if {[file isdirectory ./$i]} {
  299.         $lb insert end "$i/"
  300.       } else {
  301.         $lb insert end $i
  302.       }
  303.     } else {
  304.       $lb insert end $i
  305.     }
  306.   }
  307.  
  308.   # add any dot-files:
  309.   foreach i [lsort [glob -nocomplain .*]] {
  310.     if {$i != "." && $i != ".."} {
  311.       if { ! $J_PREFS(j_fs_fast) } {
  312.         if {[file isdirectory ./$i]} {
  313.           $lb insert end "$i/"
  314.         } else {
  315.           $lb insert end $i
  316.         }
  317.       } else {
  318.         $lb insert end $i
  319.       }
  320.     }
  321.   }
  322. }
  323.  
  324.